<?php

class m111101_015547_account_token_stat extends CDbMigration {
	public function safeUp() {
		$this->addColumn('erp_account_token_category', 'childrenCache', 'text');
		$this->addColumn('erp_user_access', 'accountTokenStat', 'boolean');
		$this
			->insert('erp_auth_item',
				array(
					'name' => 'accountTokenStat',
					'type' => CAuthItem::TYPE_ROLE,
					'bizrule' => 'return AccessControl::check("accountTokenStat");',
				));
		$this
			->insert('erp_auth_item_child',
				array(
					'parent' => 'employee',
					'child' => 'accountTokenStat',
				));
		$models = AccountTokenCategory::model()->findAll();
		foreach ($models as $model) {
			$model->updateChildrenCache();
		}
	}

	public function safeDown() {
		$this->dropColumn('erp_account_token_category', 'childrenCache');
		$this
			->delete('erp_auth_item_child',
				array(
					'and',
					'parent=:parent',
					'child=:child',
				),
				array(
					':parent' => 'employee',
					':child' => 'accountTokenStat',
				));
		$this
			->delete('erp_auth_item', 'name=:name',
				array(
					':name' => 'accountTokenStat',
				));
		$this->dropColumn('erp_user_access', 'accountTokenStat');
	}
}
